import { defineStore } from "pinia";
import { getItem, setItem } from "@/utils/store";
import { loginAPI } from "@/api/login";

export const logInStore = defineStore("logIn", {
  state() {
    return {
      token: "",
      roles: [],
    };
  },
  actions: {
    loginActions(from) {
      return new Promise((resolve, reject) => {
        console.log("登录-pinia","loginActions");
        loginAPI(from).then((res) => {
          console.log("登录-res",res);
          if (res.code == 200) {
            setItem("token", JSON.stringify(res.data.token));
            this.token = res.data.toke;
            resolve(res.code);
          }
        });
      });
    },
  },
  persist: {
    storage: sessionStorage,
    encrypt: true,
  },
});
